/**
 *  This file is part of Google App Engine suppport in NetBeans IDE.
 *
 *  Google App Engine suppport in NetBeans IDE is free software: you can
 *  redistribute it and/or modify it under the terms of the GNU General
 *  Public License as published by the Free Software Foundation, either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  Google App Engine suppport in NetBeans IDE is distributed in the hope
 *  that it will be useful, but WITHOUT ANY WARRANTY; without even the
 *  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Google App Engine suppport in NetBeans IDE.
 *  If not, see <http://www.gnu.org/licenses/>.
 */
package org.netbeans.modules.j2ee.appengine.config;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import org.netbeans.modules.j2ee.deployment.common.api.ConfigurationException;
import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
import org.netbeans.modules.j2ee.deployment.plugins.spi.config.ContextRootConfiguration;
import org.netbeans.modules.j2ee.deployment.plugins.spi.config.ModuleConfiguration;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;

/**
 * @author Michal Mocnak
 */
public class AppEngineModuleConfiguration implements ModuleConfiguration, ContextRootConfiguration {

    private final J2eeModule module;
    private final File appengineXmlFile;
    private final String name;

    public AppEngineModuleConfiguration(J2eeModule module) {
        this.module = module;
        this.appengineXmlFile = module.getDeploymentConfigurationFile("WEB-INF/appengine-web.xml");
        this.name = appengineXmlFile.getParentFile().getParentFile().getParentFile().getName();

        // Check deployment file
        checkAppEngineXml();
    }

    public Lookup getLookup() {
        return Lookups.fixed(this);
    }

    public J2eeModule getJ2eeModule() {
        return module;
    }

    public void dispose() {        
    }

    private void checkAppEngineXml() {
        if (!appengineXmlFile.exists()) {
            try {
                String lineSep = System.getProperty("line.separator"); //NOI18N
                StringBuilder content = new StringBuilder();
                content.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + lineSep);
                content.append("<appengine-web-app xmlns=\"http://appengine.google.com/ns/1.0\"" + lineSep +
                        "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" + lineSep +
                        "xsi:schemaLocation='http://kenai.com/projects/nbappengine/downloads/download/schema/appengine-web.xsd appengine-web.xsd'>"+ lineSep);
                content.append("    <application>" + name + "</application>" + lineSep);
                content.append("    <version>1</version>" + lineSep);
                content.append("</appengine-web-app>" + lineSep);                                
                createFile(appengineXmlFile, content.toString(), "UTF-8");
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }

    private void createFile(File target, String content, String encoding) throws IOException{
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(target), encoding));

        try {
            bw.write(content);
        } finally {
            bw.close();
        }
    }

    public String getContextRoot() throws ConfigurationException {
        return "/";
    }

    public void setContextRoot(String contextRoot) throws ConfigurationException {
        // Nothing to do
    }
}